<?php

class Kd_Web_File {

    private $_upload = NULL;

    public function __construct($file) {
        $this->_upload = $file;
    }

    public function hasError(){
        if($this->_upload['error'] == UPLOAD_ERR_OK){
            return TRUE;
        }
        return $this->_upload['error'];
    }

    public function getOriginalName($containEx = FALSE) {
        $result = end(explode(DIRECTORY_SEPARATOR, $this->_upload['name']));
        if($containEx){
            return substr($result, strpos('.', $result));
        }
        return $result;
    }

    public function getFileName(){
        return $this->_upload['name'];
    }

    public function getFileNameWithoutEx(){
        $arr = explode('.', $this->getFileName());
        return $arr[0];
    }

    public function getExtensionName(){
        $arr = explode('.', $this->getFileName());
        return end($arr);
    }

    public function getSize($unit = 'b') {
        switch($unit) {
            case 'b':
                return $this->_upload['size'];
            case 'k':
                return $this->_upload['size'] / 1024;
            case 'm':
                return $this->_upload['size'] / 1024 / 1024;
            default:
                return $this->_upload['size'];
        }
    }

    public function compareSize($size) {
        $unit = substr($size, -1);
        switch($unit) {
            case 'b':
                return $this->getSize('b') > substr($size, -1)? 1: -1;
            case 'k':
                return $this->getSize('k') > substr($size, -1)? 1: -1;
            case 'm':
                return $this->getSize('m') > substr($size, -1)? 1: -1;
            default:
                return $this->getSize('b') > $size? 1: -1;
        }
    }

    public function saveOriginal($directory){
        if(substr($directory, -1) != DIRECTORY_SEPARATOR){
            $directory .= DIRECTORY_SEPARATOR;
        }
        return $this->saveAs($directory.$this->getOriginalName());
    }

    public function saveRandom($directory){
        if(substr($directory, -1) != DIRECTORY_SEPARATOR){
            $directory .= DIRECTORY_SEPARATOR;
        }
        $result = $directory.time().rand(10000, 999999).'.'.$this->getExtensionName();
        if($this->saveAs($result)){
            return $result;
        }
        return FALSE;
    }

    public function saveAs($filename){
        return move_uploaded_file($this->_upload['tmp_name'], $filename);
    }

    public function saveTo($directory){
        if(substr($directory, -1) != DIRECTORY_SEPARATOR){
            $directory .= DIRECTORY_SEPARATOR;
        }
        $index = 0;
        $path = $directory.$this->getFileNameWithoutEx().'.'.$this->getExtensionName();
        while(TRUE){
            if(is_file($path)){
                $path = $directory.$this->getFileNameWithoutEx().'_'.(++$index).'.'.$this->getExtensionName();
            }else{
                break;
            }
        }
        if($this->saveAs($path)){
            return $path;
        }
        return FALSE;
    }

    public function isEmpty() {
        if(empty($this->_upload)){
            return TRUE;
        }
        return $this->_upload['size'] == 0 && empty($this->_upload['tmp_name']);
    }

    public function isInType() {
        $ex = end(explode('.', $this->_upload['tmp_name']));
        $args = func_get_args();
        return in_array($ex, $args);
    }

    public function isImage() {
        return $this->isInType("jpg", "jpeg", "gif", "png");
    }

    public function isWord() {
        return $this->isInType('doc');
    }

    public function isHtml() {
        return $this->isInType('html', 'htm');
    }
}